package com.dawn.module.cms.controller.app.department;

import com.dawn.framework.common.enums.CommonStatusEnum;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.util.object.BeanUtils;
import com.dawn.module.cms.controller.admin.department.vo.DepartmentPageReqVO;
import com.dawn.module.cms.controller.admin.department.vo.DepartmentRespVO;
import com.dawn.module.cms.controller.admin.department.vo.DepartmentSaveReqVO;
import com.dawn.module.cms.controller.admin.doctor.vo.DoctorSaveReqVO;
import com.dawn.module.cms.dal.dataobject.department.DepartmentDO;
import com.dawn.module.cms.service.department.DepartmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.dawn.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 科室信息")
@RestController
@RequestMapping("/cms/department")
@Validated
public class AppDepartmentController {

    @Resource
    private DepartmentService departmentService;

    @GetMapping("/get")
    @Operation(summary = "获得科室信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DepartmentRespVO> getDepartment(@RequestParam("id") Long id) {
        DepartmentDO department = departmentService.getDepartment(id);

        DepartmentSaveReqVO saveReqVO = new DepartmentSaveReqVO();
        saveReqVO.setId(department.getId());
        int views = 1;
        if(department.getViews() != null){
            views += department.getViews();
        }
        saveReqVO.setViews(views);
        saveReqVO.setParentId(department.getParentId());
        saveReqVO.setName(department.getName());
        saveReqVO.setSort(department.getSort());
        saveReqVO.setStatus(department.getStatus());
        departmentService.updateDepartment(saveReqVO);

        return success(BeanUtils.toBean(department, DepartmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得科室信息分页")
    public CommonResult<PageResult<DepartmentRespVO>> getDepartmentPage(@Valid DepartmentPageReqVO pageReqVO) {
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        pageReqVO.setPublish(CommonStatusEnum.ENABLE.getStatus());
        PageResult<DepartmentDO> pageResult = departmentService.getDepartmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DepartmentRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得科室列表")
    public CommonResult<List<DepartmentRespVO>> getDepartmentList(@Valid DepartmentPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        pageReqVO.setPublish(CommonStatusEnum.ENABLE.getStatus());
        List<DepartmentDO> list = departmentService.getDepartmentPage(pageReqVO).getList();
        return success( BeanUtils.toBean(list, DepartmentRespVO.class));
    }

}